package com.max.xihu.netty.nio.server;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpServerCodec;

/**
 * @Author: Max
 * @Date: 2020-03-08 21:36
 * @Description:初始化器，channel注册后，会执行里面的初始化方法
 */
public class HelloServerInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel channel) throws Exception {

        //通过channel获取管道pipeline
        ChannelPipeline pipeline = channel.pipeline();

        //通过管道添加handler
        //HttpServerCodec是由netty自己提供的助手类，可以理解为拦截器
        //当请求到服务端，我们需要做解码，响应到客户端需要做编码
        pipeline.addLast("HttpServerCodec",new HttpServerCodec());
        //添加自定义助手类，返回hello netty
        pipeline.addLast("customHandler",new CustomHandler());

    }
}
